package com.prs.crm.service.hr.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.struts2.ServletActionContext;
import org.courser.Util;

import com.lowagie.text.Rectangle;
import com.prs.crm.dao.hr.PayrollBillDao;
import com.prs.crm.domain.hr.Payroll;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Resume;
import com.prs.crm.report.ItextPdf;
import com.prs.crm.report.ReportManagerImpl;
import com.prs.crm.service.hr.PayrollBillService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class PayrollBillServiceImpl extends BaseServiceImpl<PayrollBill,PayrollBillDao> implements PayrollBillService{

	@Override
	public Collection<Integer> findPersonIds(Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().findPersonIds(payroll);
	}

	@Override
	public List<PayrollBill> getCurrentPayrollBill(Person person,
			Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().getCurrentPayrollBill(person, payroll);
	}

	@Override
	public String getPersonPayrollState(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getPersonPayrollState(payroll, person);
	}

	@Override
	public List<PayrollBill> getIncreaseItem(Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().getIncreaseItem(payroll);
	}

	@Override
	public List<PayrollBill> getReduceItem(Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().getReduceItem(payroll);
	}

	@Override
	public List<PayrollBill> getIncreaseItem(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getIncreaseItem(payroll, person);
	}

	@Override
	public List<PayrollBill> getReduceItem(Payroll payroll, Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getReduceItem(payroll, person);
	}

	@Override
	public Collection<PayrollBill> getAllPayrollBill() {
		// TODO Auto-generated method stub		
		return this.getDao().getAllPayrollBill();
	}

	@Override
	public Collection<?> getReportDataSource(Collection<Person> persons, Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().getReportDataSource(persons, payroll);
	}

	@Override
	public Collection<?> findAllPayrollBill(Collection<Person> persons,
			Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().findAllPayrollBill(persons, payroll);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String export(Collection<PayrollBill> dataList, List<Person> payrollPersons, int type, String title) {
		List<String> items = new ArrayList<String>();
		items.add("姓名");
		Hashtable hshData = new Hashtable();
		
		for(PayrollBill bill : dataList) {
			if(!items.contains(bill.getItem())) {
				items.add(bill.getItem());
			}
			String key = bill.getPerson().getId().toString() + "$" +bill.getItem();
			hshData.put(key, bill.getValue());
		}
		
		String plusPjo = "增项总计";
		String lessPjo = "减项总计";
		String authentic = "实发总计";
		items.add(plusPjo);
		items.add(lessPjo);
		items.add(authentic);
		Map<Integer,Double> plusTotalMap = new TreeMap<Integer, Double>();
		Map<Integer,Double> lessTotalMap = new TreeMap<Integer, Double>();
		
		
		for(PayrollBill bill : dataList){
			int personId = bill.getPerson().getId();
			if(bill.getType().equals("增项")){
				plusTotalMap.put(personId, plusTotalMap.get(personId) != null ? plusTotalMap.get(personId) + bill.getValue() : bill.getValue());
			}else {
				lessTotalMap.put(personId, lessTotalMap.get(personId) != null ? lessTotalMap.get(personId) + bill.getValue() : bill.getValue());
			}
		}
		
		if(plusTotalMap.size() == lessTotalMap.size()){
			for(Integer key : plusTotalMap.keySet()){
				hshData.put(key + "$" + plusPjo, plusTotalMap.get(key));
				hshData.put(key + "$" + lessPjo, lessTotalMap.get(key));
				hshData.put(key + "$" + authentic, plusTotalMap.get(key) - lessTotalMap.get(key));
			}
		}

		for(PayrollBill bill : dataList) {
			double tt = 0;
			for(Person person : payrollPersons){
				tt += (Double)hshData.get(person.getId() + "$" + bill.getItem());
			}
			hshData.put(1000000 + "$" + bill.getItem(), tt);
		}
		for(int i = 0; i < payrollPersons.size(); i++){
			double pTotal = 0;
			double lTotal = 0;
			double aTotal = 0;
			for(Integer key : plusTotalMap.keySet()){
				pTotal += (Double)hshData.get(key + "$" + plusPjo);
				lTotal += (Double)hshData.get(key + "$" + lessPjo);
				aTotal += (Double)hshData.get(key + "$" + authentic);
			}
			hshData.put(1000000 + "$" + plusPjo, pTotal);
			hshData.put(1000000 + "$" + lessPjo, lTotal);
			hshData.put(1000000 + "$" + authentic, aTotal);
		}
		String total = "总计";
		Person pe = new Person();
		pe.setId(1000000);
		Resume re = new Resume();
		re.setName(total);
		pe.setResume(re);
		payrollPersons.add(pe);
		
		ItextPdf ipdf = new ItextPdf();
		ipdf.setTitle(title);
		
		String path =Util.getFullPath(Util.combinURI( Util.createPathByDate(
				ReportManagerImpl.TMP_PATH, ServletActionContext.getServletContext()),ItextPdf.getFileName(type)),
				ServletActionContext.getServletContext());
		
		if(ReportManagerImpl.REPORTTYPE_PDF == type){
			ipdf.outPdf(new Rectangle(800,1000), path, items, (List)payrollPersons, hshData, true);
		}else if(ReportManagerImpl.REPORTTYPE_EXCEL == type){
			ipdf.export(path, title, items, (List)payrollPersons, hshData);
		}
		
		return path;
	}

	@Override
	public Collection<Integer> getPayrollIdsByPerson(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getPayrollIdsByPerson(person);
	}

	@Override
	public Collection<PayrollBill> getPayrollBillByPayroll(Payroll payroll) {
		// TODO Auto-generated method stub
		return this.getDao().getPayrollBillByPayroll(payroll);
	}


}
